#ifndef F_CASensorDemoC_h
#define F_CASensorDemoC_h

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



class EDROOMTopContext {

protected:

enum EDROOMCASensorDemoSignal { EDROOMSignalTimeout, EDROOMSignalDestroy, SDataFrame, SSignalFrame };


	friend class CASensorDemo;

	CASensorDemo &EDROOMcomponent;

	CEDROOMMessage * &Msg;

	CEDROOMMessage * &MsgBack;

	//Ports

	CEDROOMInterface & Ssi;
	CEDROOMTimingInterface & ReTx;
	CEDROOMTimingInterface & Timer;

	enum edroomStateId{I, Standby, Listening};

	enum IdTrans{Init, ReceiveData, ReceiveData_IsData, ReceiveData_CrcFail, SendFrame, SendFrame_SendData, SendFrame_SendAck, ReTxData, ReceiveSignal, ReceiveSignal_IsAck, ReceiveSignal_CrcFail,  EDROOMMEMORIA };

	//Constantes



	//Variables

	TEDROOMBool &VCrcOk;
	CDFrame &VDataFrame;
	bool_t &VEndOk;
	bool_t &VEndSend;
	FrameTypeReceived_t &VFrameSend;
	CEDROOMTimer &VIdTimer;
	int &VMessageNumber;
	int &VMessageReceived;
	CDSignal &VSignalFrame;


	//Pool

	class CEDROOMPOOLCDFrame:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDFrame(TEDROOMUInt32 elemCount,CDFrame *pMem, TEDROOMBool *pMarks);
		CDFrame	* AllocData();
	};
	CEDROOMPOOLCDFrame	& CEDROOMPoolCDFrame;
	class CEDROOMPOOLCDSignal:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDSignal(TEDROOMUInt32 elemCount,CDSignal *pMem, TEDROOMBool *pMarks);
		CDSignal	* AllocData();
	};
	CEDROOMPOOLCDSignal	& EDROOMPoolCDSignal;


	// CONSTRUCTORES

	EDROOMTopContext (CASensorDemo &act, TEDROOMBool & EDROOMpVarVCrcOk, CDFrame & EDROOMpVarVDataFrame, bool_t & EDROOMpVarVEndOk, bool_t & EDROOMpVarVEndSend, FrameTypeReceived_t & EDROOMpVarVFrameSend, CEDROOMTimer & EDROOMpVarVIdTimer, int & EDROOMpVarVMessageNumber, int & EDROOMpVarVMessageReceived, CDSignal & EDROOMpVarVSignalFrame, CEDROOMPOOLCDFrame & EDROOMpPoolCDFrame, CEDROOMPOOLCDSignal & EDROOMpPoolCDSignal );

	EDROOMTopContext ( EDROOMTopContext &contex );

public:

	bool EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans);

	void EDROOMNuevoMens () { EDROOMcomponent.EDROOMNewMsg(); }

	// Funciones Definidas por el usuario

	void	FBuildAck(byte_t *pFinalFrame);

	void	FBuildDiscovery(byte_t *pFinalFrame);

	void	FCheckFiles();

	bool_t	FGetFrames(int32_t numFile,byte_t *pMessage);

	void	FIntToChar(int32_t num,byte_t * pConvert);

	void	FReceiveData();

	void	FReceiveSignal();

	void	FRtxRandom();

	void	FSendFrame();

	void	FSTimeSend();

	void	FStoreFrame();

	bool_t	GIsAck();

	bool_t	GIsNotAck();

};

// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



class EDROOMTopState : public EDROOMTopContext {

protected:



	EDROOMTopContext::edroomStateId edroomCurrentState;

	EDROOMTopContext::edroomStateId edroomNextState;

	//Variables

	TEDROOMBool VCrcOk;
	CDFrame VDataFrame;
	bool_t VEndOk;
	bool_t VEndSend;
	FrameTypeReceived_t VFrameSend;
	CEDROOMTimer VIdTimer;
	int VMessageNumber;
	int VMessageReceived;
	CDSignal VSignalFrame;


	//Pools

	CEDROOMPOOLCDFrame	CEDROOMPoolCDFrame;
	CEDROOMPOOLCDSignal	EDROOMPoolCDSignal;


public:

	// CONSTRUCTOR

	EDROOMTopState (CASensorDemo &act, CEDROOMMemory *pEDROOMMemory  );


	// EDROOMBehaviour**********************************************

	void EDROOMBehaviour();

	// Inicio**********************************************

	void EDROOMInicio();

	// ***********************************************************************

	// SubEstado Inicial 

	// ***********************************************************************



	TEDROOMTransId EDROOMIllegada();

	// ***********************************************************************

	// SubEstado Hoja Standby

	// ***********************************************************************



	TEDROOMTransId EDROOMStandbyllegada();

	// ***********************************************************************

	// SubEstado Hoja Listening

	// ***********************************************************************



	TEDROOMTransId EDROOMListeningllegada();

};


#endif
